const mongoose = require('mongoose')
const Schema = mongoose.Schema

const CourseSchema = new Schema({
	courseid: {
		type: String
	},
	topic: {
		// required: true,
		type: String
	},
	content: [{
		title: String,
		subtitle: [String]
	}],
	createdAt: String,
  updatedAt: String
})


CourseSchema.pre('save', function (next) {
	function GMTToStr(time){
    let date = new Date(time)
    let Str = date.getFullYear() + '-' +
    toZero(date.getMonth() + 1) + '-' + 
    toZero(date.getDate()) + ' ' + 
    toZero(date.getHours()) + ':' + 
    toZero(date.getMinutes()) + ':' + 
    toZero(date.getSeconds())
    return Str
  }

  function toZero(time) {
    return time < 10 ? '0' + time : time
  }

  if (this.isNew) {
    this.createdAt = this.updatedAt = GMTToStr(Date.now())
  } else {
    this.updatedAt = GMTToStr(Date.now())
  }
	
	next()
})


mongoose.model('Course', CourseSchema)








